package com.woniu.order.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.order.client.OrderEntryQueryClient;
import com.woniu.order.client.param.SearchOrderEntryParam;
import com.woniu.order.client.result.OrderEntryQueryResultInfo;
import com.woniu.order.infra.model.OrderEntry;
import com.woniu.order.service.OrderEntryService;
import com.woniu.order.service.dto.OrderEntryDto;
import com.woniu.order.service.param.SearchOrderEntryServiceParam;
import com.woniu.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class OrderEntryQueryApiImpl implements OrderEntryQueryClient {
    @Resource
    private OrderEntryService orderEntryService;

    /**
     * 根据订单Id、SkuId查询订单条目
     *
     * @param searchOrderEntryParam
     * @return
     */
    @Override
    public Result<OrderEntryQueryResultInfo> searchOrderEntry(SearchOrderEntryParam searchOrderEntryParam) {
        log.info("根据订单Id、SkuId查询订单条目:{}", searchOrderEntryParam);
        SearchOrderEntryServiceParam searchOrderEntryServiceParam = BeanUtil.copyProperties(searchOrderEntryParam, SearchOrderEntryServiceParam.class);
        OrderEntryDto orderEntryDto = orderEntryService.searchOrderEntry(searchOrderEntryServiceParam);
        OrderEntryQueryResultInfo orderEntryQueryResultInfo = BeanUtil.copyProperties(orderEntryDto, OrderEntryQueryResultInfo.class);
        return Result.getInstance(OrderEntryQueryResultInfo.class).setData(orderEntryQueryResultInfo);
    }


    /**
     * 根据orderId查找相关的商品信息
     * @param orderId
     * @return
     */
    @Override
    public Result<List<OrderEntryQueryResultInfo>> searchOrderAllEntry(Integer orderId) {
        List<OrderEntry> orderEntryList = orderEntryService.list(new QueryWrapper<OrderEntry>().eq("order_id", orderId));
        List<OrderEntryQueryResultInfo> orderEntryQueryResultInfos = BeanUtil.copyToList(orderEntryList, OrderEntryQueryResultInfo.class);
        return Result.getInstanceList(OrderEntryQueryResultInfo.class).setData(orderEntryQueryResultInfos);
    }
}
